﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Presentation
{
    public partial class UserControlSetKeys : UserControl
    {
        public UserControlSetKeys()
        {
            InitializeComponent();
            _KeysOld = new DTO.CKeys();
            _InputShortcut = new BUS.CInputKeyShortcut();
            _InputShortcut.Install(ref txtShortcutKey);
            _OptionKey = new OptionKey();
        }

        bool _Them;
        BUS.CInputKeyShortcut _InputShortcut;
        OptionKey _OptionKey;
        public event EventHandler Close;

        public void ThemKey()
        {
            _Them = true;
            txtShortcutKey.Text = null;
            uciSetKeyOutput.KeyRunning = null;
            cmdSave.Text = "Add";
            this.cmdSave.Image = global::Presentation.Properties.Resources.add;
            cmdReset.Text = "Clear";
            _OptionKey.DelayDefault = BUS.COption.DefaultDelay;
            _OptionKey.Loop = 1;
            _OptionKey.Description = null;
            this.Show();
            txtShortcutKey.Focus();
        }

        DTO.CKeys _KeysOld;
        public void SuaKey(DTO.CKeys keys)
        {
            _Them = false;
            _KeysOld = keys;
            cmdSave.Text = "Save";
            this.cmdSave.Image = global::Presentation.Properties.Resources._45;
            cmdReset.Text = "Reset";
            txtShortcutKey.Text = _KeysOld.Shortcutkey;
            uciSetKeyOutput.KeyRunning = _KeysOld.Runningkey;
            _OptionKey.DelayDefault = _KeysOld.Delay;
            _OptionKey.Loop = _KeysOld.Loop;
            _OptionKey.Description = _KeysOld.Description;
            this.Show();
            txtShortcutKey.Focus();
        }


        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
            Close(sender,e);
        }

        private void cmdReset_Click(object sender, EventArgs e)
        {
            if (_Them)
            {
                txtShortcutKey.Text = null;
                uciSetKeyOutput.KeyRunning = null;
            }
            else
            {
                txtShortcutKey.Text = _KeysOld.Shortcutkey;
                uciSetKeyOutput.KeyRunning = _KeysOld.Runningkey;
            }
        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            if (_Them)
            {
                if (BUS.CConnect.ThemKey(txtShortcutKey.Text, uciSetKeyOutput.KeyRunning, _OptionKey.DelayDefault,_OptionKey.Loop,_OptionKey.Description))
                {
                    Close(sender, e);
                }
            }
            else
            {
                if (BUS.CConnect.SuaKey(new DTO.CKeys(txtShortcutKey.Text, uciSetKeyOutput.KeyRunning, _OptionKey.DelayDefault, _OptionKey.Loop, _OptionKey.Description), _KeysOld))
                {
                    Close(sender, e);
                }
            }
        }

        private void cmdOption_Click(object sender, EventArgs e)
        {
            _OptionKey.ShowOption();
        }
    }
}
